package leetcode.code1372;

import leetcode.helper.tree.TreeNode;

public class Solution {
	int ans;

	public int longestZigZag(TreeNode root) {
		ans = 0;
		this.f(root, 0, 0);
		return ans - 1;
	}

	private void f(TreeNode root, int l, int r) {
		if (root == null) {
			ans = Math.max(ans, Math.max(l, r));
			return;
		}
		this.f(root.left, r + 1, 0);
		this.f(root.right, 0, l + 1);
	}
}
